home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 7: Sunsite / Linux Cubed Series 7 - Sunsite Vol 1.iso / system / ups / apcd-0.5 / apcd-0 / parse.c < prev    next >
C/C++ Source or Header  |  1995-11-07  |  3KB  |  123 lines

  1. /*
  2.  * parse.c - Parsing of the configuration file
  3.  *
  4.  * Copyright (c) 1995 Pavel Korensky
  5.  * All rights reserved.
  6.  *
  7.  * Permission is hereby granted, without written agreement and without
  8.  * license or royalty fees, to use, copy, modify, and distribute this
  9.  * software and its documentation for any purpose, provided that the
  10.  * above copyright notice and the following two paragraphs appear in
  11.  * all copies of this software.
  12.  * 
  13.  * IN NO EVENT SHALL PAVEL KORENSKY BE LIABLE TO ANY PARTY FOR
  14.  * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
  15.  * OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF PAVEL
  16.  * KORENSKY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  17.  *
  18.  * PAVEL KORENSKY SPECIFICALLY DISCLAIMS ANY WARRANTIES,
  19.  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
  20.  * AND FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
  21.  * ON AN "AS IS" BASIS, AND PAVEL KORENSKY HAS NO OBLIGATION TO
  22.  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
  23.  */
  24.  
  25. /*    
  26.  * Version:
  27.  *    
  28.  * $Id: parse.c,v 1.2 1995/11/07 12:40:46 root Exp root $
  29.  *    
  30.  *    
  31.  * History:
  32.  *    
  33.  * $Log: parse.c,v $
  34.  * Revision 1.2  1995/11/07  12:40:46  root
  35.  * Version 0.5 Beta uploaded to the sunsite
  36.  *
  37.  * Revision 1.1  1995/11/01  15:24:07  root
  38.  * Initial revision
  39.  *
  40.  *    
  41.  */    
  42.  
  43.  
  44. #include "apcd.h"
  45. #include "version.h"
  46.  
  47. static char *version="$Id: parse.c,v 1.2 1995/11/07 12:40:46 root Exp root $";
  48.  
  49. int parse_config()
  50. {
  51.     FILE    *cf;
  52.     char    *buffer;
  53.         
  54.     if((cf = fopen(CONFIGFILENAME,"r")) == NULL) {
  55.         fprintf(stderr,"\n Cannot open configuration file %s\n",CONFIGFILENAME);
  56.         return(0);
  57.     }
  58.     buffer=calloc(255,sizeof(char));
  59.     while(fgets(buffer,100,cf)) {
  60.         if(buffer[0]!='#') {
  61.             if(strncmp("PORT",buffer,4) == 0) {
  62.                 sscanf(buffer,"%*s %s",use_port);
  63. #ifdef DEBUGGING
  64.                 printf("PORT %s\n",use_port);
  65. #endif
  66.                 slave=2;            /* I must be master */
  67.             }
  68.             if(strncmp("TIMEOUT",buffer,7) == 0) {
  69.                 sscanf(buffer,"%*s %d",&power_timer);
  70. #ifdef DEBUGGING
  71.                 printf("TIMEOUT %d\n",power_timer);
  72. #endif
  73.                 slave=2;
  74.             }
  75.             if(strncmp("LOGTIMEOUT",buffer,10) == 0) {
  76.                 sscanf(buffer,"%*s %d",&log_timer);
  77. #ifdef DEBUGGING
  78.                 printf("LOGTIMEOUT %d\n",log_timer);
  79. #endif
  80.                 slave=2;
  81.             }
  82.             if(strncmp("LOGFILENAME",buffer,11) == 0) {
  83.                 sscanf(buffer,"%*s %s",logfilename);
  84. #ifdef DEBUGGING
  85.                 printf("LOGFILENAME %s\n",logfilename);
  86. #endif
  87.                 slave=2;
  88.             }
  89.                                 
  90.  
  91.             if(strncmp("SLAVE",buffer,5) == 0) {
  92.                 if(num_slaves <= MAX_SLAVES) {
  93.                     sscanf(buffer,"%*s %s",slaves[num_slaves++]);
  94. #ifdef DEBUGGING
  95.                     printf("SLAVE %s   %d of %d\n",slaves[num_slaves-1],num_slaves,MAX_SLAVES);
  96. #endif
  97.                 }
  98.             }
  99.             if(strncmp("MASTER",buffer,6) == 0) {
  100.                 if(slave != 0) {
  101.                     printf("I can't be both MASTER and SLAVE\n");
  102.                     return(0);
  103.                 }
  104.                 sscanf(buffer,"%*s %s",master_name);
  105. #ifdef DEBUGGING
  106.                 printf("MASTER %s\n",master_name);
  107. #endif
  108.                 slave=1;
  109.             }
  110.         }
  111.     }
  112.  
  113. #ifdef DEBUGGING
  114.     if(slave==1) printf("I am slave\n");
  115.     if(slave==2) printf("I am master\n");
  116.     if(slave==0) printf("slave variable is 0 !!!\n");
  117. #endif
  118.  
  119.     fclose(cf);
  120.     return (slave);
  121. }
  122.     
  123.